package com.one.consumer.main.presenter;

import android.content.Context;

import com.one.common.model.bean.PayWXBean;
import com.one.common.model.http.callback.ObserverOnResultListener;
import com.one.common.presenter.BaseApiPresenter;
import com.one.common.utils.PayUtils;
import com.one.common.utils.Toaster;
import com.one.common.view.base.BaseActivity;
import com.one.common.view.base.IListView;
import com.one.consumer.main.model.item.OrderItem;
import com.one.consumer.order.model.OrderModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/10/21 下午5:13
 * description ：
 **/
public class OrderListPresenter extends BaseApiPresenter<IListView, OrderModel> {

    public OrderListPresenter(IListView mView, Context mContext) {
        super(mView, mContext, new OrderModel((BaseActivity) mContext));
    }

    /**
     * 获取订单列表
     */
    public void getOrderList(String state) {
        mModel.getOrderList(mView.getPage() + "", state, new ObserverOnResultListener<ArrayList<OrderItem>>() {
            @Override
            public void onResult(ArrayList<OrderItem> orderItems) {
                if (mView != null) {
                    mView.loadSuccess(orderItems);
                }
            }
        });
    }

    /**
     * 搜索订单
     */
    public void searchOrder(String name) {
        mModel.searchOrder(mView.getPage() + "", name, new ObserverOnResultListener<ArrayList<OrderItem>>() {
            @Override
            public void onResult(ArrayList<OrderItem> orderItems) {
                if (mView != null) {
                    mView.loadSuccess(orderItems);
                }
            }
        });
    }


    /**
     * 取消订单
     */
    public void cancelOrder(String id) {
        mModel.cancelOrder(id, new ObserverOnResultListener<Object>() {
            @Override
            public void onResult(Object o) {
                if (mView != null) {
                    mView.refresh();
                }
            }
        });
    }

    /**
     * 支付订单
     */
    public void payOrder(String id) {
        mModel.payOrder(id, new ObserverOnResultListener<PayWXBean>() {
            @Override
            public void onResult(PayWXBean o) {
                if (mView != null && o != null) {
                    PayUtils.getInstance(mActivity).wxPay(o)
                            .setIPayListener(new PayUtils.IPayListener() {
                                @Override
                                public void onSuccess() {
                                    mView.refresh();
                                }

                                @Override
                                public void onFail() {

                                }

                                @Override
                                public void onCancel() {
                                    Toaster.showLongToast("取消支付");
                                }
                            });
                }
            }
        });
    }

    /**
     * 确认订单
     */
    public void confirmOrder(String id) {
        mModel.confirmOrder(id, new ObserverOnResultListener<Object>() {
            @Override
            public void onResult(Object o) {
                if (mView != null) {
                    mView.refresh();
                }
            }
        });
    }

    /**
     * 评价订单
     */
    public void evaluateOrder() {
        Map<String, String> params = new HashMap<>();
        mModel.evaluateOrder(params, new ObserverOnResultListener<Object>() {
            @Override
            public void onResult(Object o) {
                if (mView != null) {

                }
            }
        });
    }

    /**
     * 删除订单
     */
    public void deleteOrder(String id) {
        mModel.deleteOrder(id, new ObserverOnResultListener<Object>() {
            @Override
            public void onResult(Object o) {
                if (mView != null) {
                    mView.refresh();
                }
            }
        });
    }
}
